package it.cums.core.util;

import it.cums.core.controller.ApplicationController;
import it.cums.core.model.Customer;
import it.cums.core.model.Customers;
import it.cums.core.Activator;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class CustomerManager {
	private static CustomerManager customerManager;
	
	private CustomerManager() {
	}
	
	public static CustomerManager getInstance() {
		if (customerManager == null) {
			customerManager = new CustomerManager();
		}
		return customerManager;
	}
	
	public void createAndStoreCustomer(Customers customers, String name, String referencePerson, String taxNumber, String address, String cap, String location, String country, String phone, String mobilePhone, String email, String webSite, String deliveryAddress, String deliveryCap, String deliveryLocation, String deliveryCountry, String notes) throws Exception {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			Customer c = new Customer();
			c.setName(name);
			c.setReferencePerson(referencePerson);
			c.setTaxNumber(taxNumber);
			c.setAddress(address);
			c.setLocation(location);
			c.setCap(cap);
			c.setCountry(ApplicationController.getInstance().getCountry(country));
			c.setPhone(phone);
			c.setMobilePhone(mobilePhone);
			c.setEmail(email);
			c.setWebSite(webSite);
			c.setDeliveryAddress(deliveryAddress);
			c.setDeliveryCap(deliveryCap);
			c.setDeliveryLocation(deliveryLocation);
			if (deliveryCountry.equals("")) {
				c.setDeliveryCountry(null);
			} else {
				c.setDeliveryCountry(ApplicationController.getInstance().getCountry(deliveryCountry));
			}
			c.setNotes(notes);
			session.save(c);
			tx.commit();
			customers.addCustomer(c);
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			MessageCreator.showErrorMessage("Customer can't be added to the database. Please verify your database settings");
			HibernateUtil.rollback(tx);
			throw e;
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	public void updateCustomer(Customer customer, String name, String referencePerson, String taxNumber, String address, String cap, String location, String country, String phone, String mobilePhone, String email, String webSite, String deliveryAddress, String deliveryCap, String deliveryLocation, String deliveryCountry, String notes) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			customer.setName(name);
			customer.setReferencePerson(referencePerson);
			customer.setTaxNumber(taxNumber);
			customer.setAddress(address);
			customer.setCap(cap);
			customer.setLocation(location);
			customer.setCountry(ApplicationController.getInstance().getCountry(country));
			customer.setPhone(phone);
			customer.setMobilePhone(mobilePhone);
			customer.setEmail(email);
			customer.setWebSite(webSite);
			customer.setDeliveryAddress(deliveryAddress);
			customer.setDeliveryCap(deliveryCap);
			customer.setDeliveryLocation(deliveryLocation);
			if (deliveryCountry.equals("")) {
				customer.setDeliveryCountry(null);
			} else {
				customer.setDeliveryCountry(ApplicationController.getInstance().getCountry(deliveryCountry));
			}
			customer.setNotes(notes);
			session.update(customer);
			tx.commit();
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			MessageCreator.showErrorMessage("Customer can't be updated in the database!");
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> listCustomers() {
		Session session = null;
		Transaction tx = null;
		List<Customer> customers = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
		    tx = session.beginTransaction();
		    customers = session.createQuery("from Customer as c order by c.name").list();
		    tx.commit();
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
		return customers;
    }
	
	public boolean deleteCustomer(Customers customers, Customer customer) {
		boolean successful = false;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			OrderManager.getInstance().deleteOrders(session, customer);
			session.delete(customer);
			tx.commit();
			customers.removeCustomer(customer);
			successful = true;
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
			MessageCreator.showErrorMessage("Customer '" + customer.getName() + "' can't be removed from the database!");
			HibernateUtil.rollback(tx);
		} finally {
			HibernateUtil.closeSession(session);
		}
		return successful;
	}
}